
package ar.com.visualAORE.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReaderWriter 
{
    static FileReader input;
    
   public static void openFile(String filePath) 
   {
        try 
        {
            File file = new File(filePath);
            input = new FileReader(file);
        } catch ( FileNotFoundException e ) {
            System.out.println(e.getMessage());
        }       
    }
    
    public static String readFile(String filePath) 
    {	
    	openFile(filePath);
        char[] buffer = new char[512];					// buffer de lectura
        int longitud;                  
        String content = "";
        try 
        {
        	while ((longitud = input.read(buffer)) > 0) 
        		content = content.concat(new String(buffer,0,longitud));
         } catch ( IOException e ) {
           System.out.println(e.getMessage());
        }
        closeFile(); 
        return content; 
    }
    
    void writeFile(String document, String path) {
        try {
        	File file = new File(path);
        	FileWriter output = new FileWriter(file);
            
        	output.write(document);
        	output.close();
        	
            System.out.println( ">> graba string en archivo" );
        } catch ( IOException e ) {
            System.out.println(e.getMessage());
        }        
    }
    
    public static void closeFile() 
    {
        try 
        {
            input.close();
            System.out.println("----------------" );            
            System.out.println("Cierra el archivo." );
        } catch ( IOException e ) {
            System.out.println(e.getMessage());
        }        
    }        
}